﻿namespace KinonekoSoftware.CodariaDB.Documents.Theories.Rarities
{
    public class TheoryEngine_Rarity : TheoryEngine<Rarity>
    {
        private const string Object_Theory_Rarity = "KinonekoSoftware.CodariaDB.Documents.Theories.Rarities.TheoryOfRarity";

        public void Add(Rarity element)
        {
            if (element is null)
            {
                return;
            }

            //
            //
            ((IGlossaryOperation)element).Add(this);

            //
            //
            InsideDB.Insert(element);
            InternalCollection.Add(element);
            Modified();
        }
        

        public void Update(Rarity element)
        {
            if (element is null)
            {
                return;
            }

            //
            //
            ((IGlossaryOperation)element).Update(this);

            //
            //
            InsideDB.Update(element);
            Modified();
        }

        public void Remove(Rarity element)
        {
            if (element is null)
            {
                return;
            }

            //
            //
            ((IGlossaryOperation)element).Remove(this);

            //
            //
            InsideDB.Delete(element.Id);
            InternalCollection.Remove(element);
            Modified();
        }
        
        
        public void Clear()
        {
            //
            //
            foreach (var element in InternalCollection)
            {
                //
                //
                ((IGlossaryOperation)element).Remove(this);
                
                //
                //
                InsideDB.Delete(element.Id);
                
                //
                //
                InternalCollection.Remove(element);
            }
            
            
            Modified();
        }
        
        protected override void Sort(IList<Rarity> collection)
        {
            ((List<Rarity>)collection).Sort(CompareTo);
        }

        private static int CompareTo(Rarity x, Rarity y)
        {
            var x1 = (x?.Level) ?? 0;
            var y1 = (y?.Level) ?? 0;
            return -(x1 - y1);
        }
        
        
        
        protected override void OnInitialize()
        {
#if DEBUG
            Add(new Rarity { Id = ID.Get(), Name = "史诗", Foreground = "#FFFFFF", Stroke = "#FFAB12", Background = "#cf4747", Level = 5 });
            Add(new Rarity { Id = ID.Get(), Name = "传奇", Foreground = "#FFFFFF", Stroke = "#FFFFFF", Background = "#ff9600", Level = 4 });
            Add(new Rarity { Id = ID.Get(), Name = "稀有", Foreground = "#FFFFFF", Stroke = "#FFAB12", Background = "#9132c8", Level = 3 });
            Add(new Rarity { Id = ID.Get(), Name = "精英", Foreground = "#FFFFFF", Stroke = "#FFAB12", Background = "#2f78ff", Level = 2 });
            Add(new Rarity { Id = ID.Get(), Name = "普通", Foreground = "#000000", Stroke = "#df4952", Background = "#d0d0d0", Level = 1 });

#else
            Add(new Rarity { Id = ID.Get(), Name = "史诗", Foreground = "#FFFFFF", Stroke = "#FFAB12", Background = "#cf4747", Level = 5 });
            Add(new Rarity { Id = ID.Get(), Name = "传奇", Foreground = "#FFFFFF", Stroke = "#FFFFFF", Background = "#ff9600", Level = 4 });
            Add(new Rarity { Id = ID.Get(), Name = "稀有", Foreground = "#FFFFFF", Stroke = "#FFAB12", Background = "#9132c8", Level = 3 });
            Add(new Rarity { Id = ID.Get(), Name = "精英", Foreground = "#FFFFFF", Stroke = "#FFAB12", Background = "#2f78ff", Level = 2 });
            Add(new Rarity { Id = ID.Get(), Name = "普通", Foreground = "#000000", Stroke = "#df4952", Background = "#d0d0d0", Level = 1 });
#endif
        }

        public override string EngineID => EngineNames.Theory_Rarity;
        public override string GetCollectionName() => CollectionNames.Theory_RarityDB;
    }
}